package edu.mta.pokerstats;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.cloudfoundry.services.CloudFoundryServices;
import org.cloudfoundry.services.ServiceManager;

public class GameRecord {
	private int m_GameId;
	private int m_PlayerId;
	private int m_NumberOfRebuys;
	private int m_WonInGame;
	private int m_Position;
	private int m_Knockouts;

	public GameRecord(int i_GameID, int i_PlayerId, int i_NumberOfRebuys,
			int i_WonInGame, int i_Position, int i_Knockouts) {
		m_GameId = i_GameID;
		m_PlayerId = i_PlayerId;
		m_NumberOfRebuys = i_NumberOfRebuys;
		m_WonInGame = i_WonInGame;
		m_Position = i_Position;
		m_Knockouts = i_Knockouts;
	}

	public GameRecord(ResultSet i_RS) {
		try {
			m_GameId = i_RS.getInt("gameID");
			m_PlayerId = i_RS.getInt("playerID");
			m_NumberOfRebuys = i_RS.getInt("numberOfRebuys");
			m_WonInGame = i_RS.getInt("wonInGame");
			m_Position = i_RS.getInt("position");
			m_Knockouts = i_RS.getInt("knockouts");
		} catch (SQLException e) {
			new GameRecord(0, 0, 0, 0, 0, 0);
		}
	}

	public int getGameId() {
		return m_GameId;
	}

	public void setGameId(int m_GameId) {
		this.m_GameId = m_GameId;
	}

	public int getPlayerId() {
		return m_PlayerId;
	}

	public void setPlayerId(int m_PlayerId) {
		this.m_PlayerId = m_PlayerId;
	}

	public int getNumberOfRebuys() {
		return m_NumberOfRebuys;
	}

	public void setNumberOfRebuys(int m_NumberOfRebuys) {
		this.m_NumberOfRebuys = m_NumberOfRebuys;
	}

	public int getWonInGame() {
		return m_WonInGame;
	}

	public void setWonInGame(int m_WonInGame) {
		this.m_WonInGame = m_WonInGame;
	}

	public int getPosition() {
		return m_Position;
	}

	public void setPosition(int m_Position) {
		this.m_Position = m_Position;
	}
	
	public int getKnockouts() {
		return m_Knockouts;
	}
	
	public void setKnockouts(int m_Knockouts) {
		this.m_Knockouts = m_Knockouts;
	}

	public boolean InsertToDatabase() {
		boolean success = false;
		Connection connection = null;
		int rowsAffected = 0;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = getInsertString();
			rowsAffected = s.executeUpdate(query);
			if (rowsAffected == 1) {
				success = true;
			}
		} catch (Exception e) {
			success = false;
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return success;
	}

	public boolean UpdateInDatabase() {
		return ServiceManager.INSTANCE.UpdateQuery(getUpdateString());
	}

	public boolean DeleteFromDatabase() {
		String query = "DELETE FROM gameRecords WHERE gameID = "
				+ m_GameId + " and playerID = " + m_PlayerId;
		ServiceManager.INSTANCE.RunGenerelQuery(query);

		return true;
	}

	private String getInsertString() {
		String result = "insert into gameRecords values(" + m_GameId + ","
				+ m_PlayerId + "," + m_NumberOfRebuys + "," + m_WonInGame + ","
				+ m_Position + "," + m_Knockouts + ")";

		return result;
	}

	private String getUpdateString() {
		String result = "update gameRecords SET " + "numberOfRebuys = "
				+ m_NumberOfRebuys + "," + "wonInGame = " + m_WonInGame + ","
				+ "position = " + m_Position + "," + "knockouts = "+ m_Knockouts 
				+ " WHERE gameID = " + m_GameId
				+ " and playerID = " + m_PlayerId;

		return result;
	}

	public static GameRecord getGameRecordByIds(int i_GameId, int i_PlayerId) {
		GameRecord returnValue = null;
		Connection connection = null;
		try {
			connection = (Connection) ServiceManager.INSTANCE
					.getInstance(CloudFoundryServices.MYSQL);
			Statement s = connection.createStatement();
			String query = "Select * from gameRecords where gameID = "
					+ i_GameId + " and playerID = " + i_PlayerId;
			ResultSet rs = s.executeQuery(query);
			rs.first();
			returnValue = new GameRecord(rs);
		} catch (Exception e) {
			returnValue = null;
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					if (!connection.isClosed()) {
						connection.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return returnValue;
	}
}